#include "serverHead/sql_command.h"

char* createSalt(){
    char* salt=(char*)malloc(sizeof(char)*21);
    const char charset[]="qwertyuiopasdfghjklzxcvbnm$QWERTYUIOPASDFGHJKLZXCVBNM#0123456789";
    srand(time(NULL));
    int set_len=strlen(charset);

    for(int i=0;i<20;++i){
        int index=rand()%set_len;
        salt[i]=charset[index];
    }

    salt[20]='\0';


    return salt;
}

char *createPasswd(char *passwd, char *salt){
    char *cryptPasswd = (char *)calloc(1, 128 * sizeof(char));
    cryptPasswd = crypt(passwd, salt);
    return cryptPasswd;
}

int mySignup(char *data){
    
    char *userName = strtok(data, "\n");
    char *realPasswd = strtok(NULL, " ");//用户真实密码
    char *salt = createSalt();
    char *cryptPasswd = createPasswd(realPasswd, salt);//加密后的密文密码
    char *info[] = {userName, salt, cryptPasswd};//用来循环将各种信息装进sql语句
    char sql[4096] = "insert into userinfo (user, salt, passwd, tomb) values (";
    //拼接sql语句
    for(int i = 0; i < 3; ++i){
        strcat(sql, "'");
        strcat(sql, info[i]);
        strcat(sql, "'");
        strcat(sql, ",");
    }
    strcat(sql, "'");
    strcat(sql, "0");
    strcat(sql, "'");
    strcat(sql, ")");
    //strcat(sql, ";");

    //将用户信息存进数据库
    MYSQL *mysql = mysql_init(NULL);
    char *host = "localhost";
    char *user = "root";
    char *passwd = "284613";//数据库的登录密码
    char *database = "test";
    MYSQL *ret = mysql_real_connect(mysql, host, user, passwd, database, 0, NULL, 0);//建立连接
    if(!ret){
        printf("Error:%s\n", mysql_error(mysql));
        return -1;
    }
    const char *order = sql;
    int qret = mysql_query(mysql, order);
    mysql_close(mysql);
    return 0;
}

int mySignin(char *data, char *username){
    //传进来的用户名和密码
    char *name = strtok(data, "\n");
    for(unsigned int i = 0; i < strlen(name); ++i){
        username[i] = name[i];
    }

    //username = strtok(data, "\n");
    printf("username为%s\n", username);
    char *realPasswd = strtok(NULL, " ");//用户真实密码
    //按名字查表得到密码信息
    MYSQL *mysql = mysql_init(NULL);
    char *host = "localhost";
    char *user = "root";
    char *passwd = "284613";//数据库的登录密码
    char *database = "test";
    MYSQL *ret = mysql_real_connect(mysql, host, user, passwd, database, 0, NULL, 0);//建立连接
    if(!ret){
        printf("Error:%s\n", mysql_error(mysql));
        return -1;
    }
    char sql[4096] = "select user,salt,passwd,tomb from userinfo where user = ";
    strcat(sql, "'");
    strcat(sql, username);
    strcat(sql, "'");//拼接查询sql语句

    const char *order = sql;
    int qret = mysql_query(mysql, order);
    if(qret){
        printf("Error:%s\n", mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(result);
    
    //新加
    int cmp = strcmp(row[3], "1");
    if(cmp == 0){
        printf("用户不存在！\n");
        return -1;
    }
    //新加结束


    printf("表中盐值为%s\n", row[1]);
    char *cryptPasswd = createPasswd(realPasswd, row[1]);//计算加密后的密文密码
    //按名字查表得到密码信息
    

    //测试开始
    printf("计算密文为%s\n", cryptPasswd);
    printf("表中密文为%s\n", row[2]);
    //测试结束
    
    int isSame = strcmp(cryptPasswd, row[2]);//row[1]是存在数据库中的加密密码, isSame为0时密码正确

    mysql_close(mysql);
    
    return isSame;
}

void mySigndel(char *username){
    MYSQL *mysql = mysql_init(NULL);
    mysql_real_connect(mysql, "localhost", "root", "284613", "test", 0, NULL, 0);
    char sql[4096] = "update userinfo set tomb = 1 where user = ";
    strcat(sql, "'");
    strcat(sql, username);
    strcat(sql, "'");
    const char *order = sql;
    int ret = mysql_query(mysql, order);
    if(ret){
        printf("1\n");
    }
    mysql_close(mysql);
    printf("执行完成\n");
}

void myLs(char *pathname, char *buf){
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(mysql, "localhost", "root", "284613", "test", 0, NULL, 0);

    //1、先找到满足当前pathname的level
    //2、再找出当前用户level+1的信息，把这些信息传进buf
   
    //拼接第一条查询指令
    //select level from filesys where path = 'pathname'
    char sql1[4096] = "select level from filesys where path = ";
    strcat(sql1, "'");
    strcat(sql1, pathname);
    strcat(sql1, "'");

    const char *order1 = sql1;
    int qret = mysql_query(mysql, order1);
    if(qret){
        printf("Error:%s\n", mysql_error(mysql));
    }
    MYSQL_RES *result1 = mysql_store_result(mysql);
    MYSQL_ROW row1 = mysql_fetch_row(result1);
    int level = atoi(row1[0]) + 1;//level是要输出的文件层级
    char buff[1024] = {0};
    sprintf(buff, "%d", level);//将level转换为字符串
   
    //拼接第二条查询指令
    //select level from filesys where level = '3'
    char sql2[4096] = "select filename from filesys where level = ";
    strcat(sql2, "'");
    strcat(sql2, buff);
    strcat(sql2, "'");
    
    const char *order2 = sql2;
    qret = mysql_query(mysql, order2);
    if(qret){
        printf("Error:%s\n", mysql_error(mysql));
    }
    MYSQL_RES *result2 = mysql_store_result(mysql);
    unsigned int nums = mysql_num_rows(result2);
    for(unsigned int i = 0; i < nums; ++i){
        MYSQL_ROW row = mysql_fetch_row(result2);
        if(i == 0){
            strcpy(buf, row[0]);
            strcat(buf," ");
        }else{
            strcat(buf, row[0]);
            strcat(buf, " ");
        }
    } 
}

int myMkdir(char* pathname,char* data){
    if(strlen(data)==0){
        return -1;
    }
    MYSQL* mysql=mysql_init(NULL);
    MYSQL* ret=mysql_real_connect(mysql,"localhost","root","284613","test",0,NULL,0);
    if(ret==NULL){
        return -1;
    }
    // usr/dir1 mkdir dir2
    char path[128];
    strcpy(path,pathname);
    char* cret1=strrchr(path,'/');
    int cpLen=strlen(path)-strlen(cret1);
    char* fath_name=strtok(cret1,"/");
    char  fath_path[128];
    strxfrm(fath_path,path,cpLen);
    //select owner level from filesys where path='usr/' and filename='dir1'
    char sql1[256]="select owner level from filesys where path='";
    strcat(sql1,fath_path);
    strcat(sql1,"' and filename ='");
    strcat(sql1,fath_name);
    strcat(sql1,"'");
    int qret=mysql_query(mysql,sql1);
    if(qret!=0){
        return -1;
    }
    MYSQL_RES* result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    char* owner=row[0];
    char* level=row[1];
    int i=atoi(level);
    ++i;
    char mylevel[32];
    bzero(mylevel,sizeof(mylevel));
    sprintf(mylevel,"%d",i);
    //insert into filesys (owner,level,type,filename,fathername,path,tomb)
    //values ('owner','mylevel','1','data','fath_name','pathname','0')
    char sql2[256]="insert into filesys (owner,level,type,filename,fathername,path,tomb) values ('";
    strcat(sql2,owner);
    strcat(sql2,"','");
    strcat(sql2,mylevel);
    strcat(sql2,"','1','");
    strcat(sql2,data);
    strcat(sql2,"','");
    strcat(sql2,fath_name);
    strcat(sql2,"','");
    strcat(sql2,pathname);
    strcat(sql2,"','0'");
    qret=mysql_query(mysql,sql2);
    if(qret!=0){
        return -1;
    }
    mysql_close(mysql);
    return 0;
}

int myRmdir(char* pathname,char* data){
    if(strlen(data)==0){
        return -1;
    }
    MYSQL* mysql=mysql_init(NULL);
    MYSQL* ret=mysql_real_connect(mysql,"localhost","root","284613","test",0,NULL,0);
    if(ret==NULL){
        return -1;
    }

    char sql1[256]="select type from filesys where path='";
    strcat(sql1,pathname);
    strcat(sql1,"' and tomb ='0' and filename ='");
    strcat(sql1,data);
    strcat(sql1,"'");

    int qret=mysql_query(mysql,sql1);
    if(qret!=0){
        return -1;
    }
    MYSQL_RES* result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    char* type=row[0];
    //is file
    if(strcmp(type,"0")==0){
        return -1;
    }
    //is dir
    if(strcmp(type,"1")==0){
         char next_pathname[256];
         strcpy(next_pathname,pathname);
         strcat(next_pathname,"/");
         strcat(next_pathname,data);
         char sql5[256]="select filename from filesys where fathername='";
         strcat(sql5,data);
         strcat(sql5,"' and path='");
         strcat(sql5,next_pathname);
         strcat(sql5,"'");
         qret=mysql_query(mysql,sql5);
         if(qret!=0){
             printf("error:%s\n",mysql_error(mysql));
             return -1;
         }
         MYSQL_RES* res=mysql_store_result(mysql);
         MYSQL_ROW row1;
         while((row=mysql_fetch_row(res))!=NULL){
             char* filename=row[0];
             char sql6[256]="updata filesys set tomb ='1' where path='";
             strcat(sql6,next_pathname);
             strcat(sql6,"' and filename='");
             strcat(sql6,filename);
             strcat(sql6,"'");
             qret=mysql_query(mysql,sql6);
             if(qret!=0){
                 printf("error:%s\n",mysql_error(mysql));
                 return -1;
             }
         }
         char sql[256]="updata filesys set tomb='1' where path='";
         strcat(sql,pathname);
         strcat(sql,"' and filename='");
         strcat(sql,data);
         strcat(sql,"'");
         qret=mysql_query(mysql,sql);
         if(qret!=0){
             printf("error:%s\n",mysql_error(mysql));
             return -1;
          }
         mysql_close(mysql);
         return 0;
     }
     mysql_close(mysql);
     return 0;
}

int myRemove(char* pathname,char* data){
    if(strlen(data)==0){
        return -1;
    }
    MYSQL* mysql=mysql_init(NULL);
    MYSQL* ret=mysql_real_connect(mysql,"localhost","root","284613","test",0,NULL,0);
    if(ret==NULL){
        return -1;
    }
    char sql1[256]="select type from filesys where path='";
    strcat(sql1,pathname);
    strcat(sql1,"' and tomb ='0' and filename ='");
    strcat(sql1,data);
    strcat(sql1,"'");

    int qret=mysql_query(mysql,sql1);
    if(qret!=0){
        return -1;
    }
    MYSQL_RES* result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    char* type=row[0];
    //is file
    if(strcmp(type,"0")==0){
        char sql4[256]="updata filesys set tomb='1' where path='";
        strcat(sql4,pathname);
        strcat(sql4,"' and filename =' ");
        strcat(sql4,data);
        strcat(sql4,"'");
        qret=mysql_query(mysql,sql4);
        if(qret!=0){
            printf("error:%s\n",mysql_error(mysql));
            return -1;
        }
        mysql_close(mysql);
        return 0;
    }
    //is dir
    if(strcmp(type,"1")==0){
        char next_pathname[128];
        strcpy(next_pathname,pathname);
        strcat(next_pathname,"/");
        strcat(next_pathname,data);
        char sql5[256]="select filename from filesys where fathername='";
        strcat(sql5,data);
        strcat(sql5,"' and path='");
        strcat(sql5,next_pathname);
        strcat(sql5,"'");
        qret=mysql_query(mysql,sql5);
        if(qret!=0){
            printf("error:%s\n",mysql_error(mysql));
            return -1;
        }
        MYSQL_RES* res=mysql_store_result(mysql);
        MYSQL_ROW row1;
        while((row=mysql_fetch_row(res))!=NULL){
            char* filename=row[0];
            char sql6[256]="updata filesys set tomb ='1' where path='";
            strcat(sql6,next_pathname);
            strcat(sql6,"' and filename='");
            strcat(sql6,filename);
            strcat(sql6,"'");
            qret=mysql_query(mysql,sql6);
            if(qret!=0){
                printf("error:%s\n",mysql_error(mysql));
                return -1;
            }
        }
        char sql[256]="updata filesys set tomb='1' where path='";
        strcat(sql,pathname);
        strcat(sql,"' and filename='");
        strcat(sql,data);
        strcat(sql,"'");
        qret=mysql_query(mysql,sql);
        if(qret!=0){
            printf("error:%s\n",mysql_error(mysql));
            return -1;
        }
        mysql_close(mysql);
        return 0;
    }
    mysql_close(mysql);
    return 0;
}

